package com.ruoyi.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.order.domain.*;
import com.ruoyi.order.mapper.CheOrderUserShopMapper;
import com.ruoyi.order.mapper.CheServiceMapper;
import com.ruoyi.order.service.ICheOrderUserShopService;
import com.ruoyi.rescue.api.ReCarApiService;
import com.ruoyi.rescue.api.domain.CheCar;
import com.ruoyi.shop.api.CheApiShopService;
import com.ruoyi.user.api.CheApiCouponService;
import com.ruoyi.user.api.CheApiUserService;
import com.ruoyi.user.api.domain.CheCoupon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @ClassName CheOrderAndUserServiceImpl
 * @Description TODO
 * @Author liuzhiqiang
 * @Date 2024年06月05日 14:45
 * @Version 1.0
 */
@Service
public class CheOrderUserShopServiceImpl implements ICheOrderUserShopService {
    @Autowired
    private CheApiUserService cheApiUserService;
    @Autowired
    private CheApiShopService cheApiShopService;
    @Autowired
    private CheOrderUserShopMapper cheOrderMapper;
    @Autowired
    private CheServiceMapper cheServiceMapper;
    @Autowired
    private ReCarApiService reCarApiService;
    @Autowired
    private CheApiCouponService cheApiCouponService;



    @Override
    public List<CheOrderUserShop> selectCheOrderList(CheOrder cheOrder) {
        List<CheOrderUserShop> list =new ArrayList<>();
        List<CheOrder> cheOrders = cheOrderMapper.selectOrderList(cheOrder);
        for(CheOrder cho :cheOrders){
            CheOrderUserShop che1=new CheOrderUserShop();
            AjaxResult info1 = cheApiShopService.getInfo(cho.getShopId(), SecurityConstants.INNER);
            AjaxResult info2 = cheApiUserService.getInfo(cho.getUserId(), SecurityConstants.INNER);
            //商家
            LinkedHashMap ss=(LinkedHashMap)info1.get("data");
            CheShop dataShop = BeanUtil.mapToBean(ss, CheShop.class, true);
            //用户
            LinkedHashMap uu = (LinkedHashMap)info2.get("data");
            CheUser dataUser= BeanUtil.mapToBean(uu, CheUser.class, true);

            che1.setId(cho.getId());
            che1.setNumber(cho.getNumber());//订单号
            che1.setShopId(cho.getShopId());
            che1.setShopName(dataShop.getShopName());//商家名

            //用户
            che1.setUserId(cho.getUserId());
            che1.setUserName(dataUser.getRealName());

            //车
            che1.setCarId(cho.getCarId());
//            System.out.println(cho.getCarId());
            AjaxResult info3 = reCarApiService.getInfo(cho.getCarId(), SecurityConstants.INNER);
            LinkedHashMap cc = (LinkedHashMap) info3.get("data");
            CheCar cheCar = BeanUtil.mapToBean(cc, CheCar.class, true);
            che1.setCarNumber(cheCar.getCarNumber());//车牌
            //优惠券信息
            che1.setCouponId(cho.getCouponId());//优惠价ID
            AjaxResult info4 = cheApiCouponService.getById(cho.getCouponId(),SecurityConstants.INNER);
            LinkedHashMap cou = (LinkedHashMap) info4.get("data");
            CheCoupon cheCoupon = BeanUtil.mapToBean(cou, CheCoupon.class, true);
            che1.setCouponName(cheCoupon.getName());//优惠卷名
            che1.setCouponMoney(cheCoupon.getReduce());//减少金额



            //服务项目
            List<CheServiceName> cheServiceNames = cheServiceMapper.selectOrderServiceNameById(cho.getId());

            String str=null;
            for (CheServiceName ser:cheServiceNames){
                str = ser.getName();
//                Double newPrice = ser.getNewPrice();
//                ne+=newPrice;
            }
            che1.setServiceName(str);//服务名称

            che1.setTotalPrice(cho.getTotalPrice());//总价
            che1.setRealPrice(cho.getRealPrice());//实付
//            BigDecimal realPrice = cho.getRealPrice();

//            BigDecimal coupon = big.subtract(realPrice);//优惠
//            che1.setCoupon(coupon);
            che1.setVerificationCode(cho.getVerificationCode());//核销码
            che1.setQrcode(cho.getQrcode());//核销二维码
            che1.setCreateTime(cho.getCreateTime());//下单时间
            che1.setSubscribeTime(cho.getSubscribeTime());//预约时间
            che1.setVerificationTime(cho.getVerificationTime());//核销时间

            //状态
            String state=null;
            if(cho.getState().equals("0")){
                state="待支付";
            }
            if(cho.getState().equals("1")){
                state="待使用";
            }
            if(cho.getState().equals("2")){
                state="待评价";
            }
            if(cho.getState().equals("3")){
                state="已取消";
            }
            if(cho.getState().equals("4")){
                state="已退款";
            }
            if(cho.getState().equals("5")){
                state="已评价";
            }
            che1.setState(state);

            list.add(che1);
        }

        return list;
    }

    @Override
    public CheOrderUserShop selectCheOrderById(Long id) {
        CheOrder cho = cheOrderMapper.selectCheOrderById(id);
        CheOrderUserShop che1=new CheOrderUserShop();
        AjaxResult info1 = cheApiShopService.getInfo(cho.getShopId(), SecurityConstants.INNER);
        AjaxResult info2 = cheApiUserService.getInfo(cho.getUserId(), SecurityConstants.INNER);
        //商家
        LinkedHashMap ss=(LinkedHashMap)info1.get("data");
        CheShop dataShop = BeanUtil.mapToBean(ss, CheShop.class, true);
        //用户
        LinkedHashMap uu = (LinkedHashMap)info2.get("data");
        CheUser dataUser= BeanUtil.mapToBean(uu, CheUser.class, true);

        che1.setId(cho.getId());
        che1.setNumber(cho.getNumber());//订单号
        che1.setShopId(cho.getShopId());
        che1.setShopName(dataShop.getShopName());//商家名

        //用户
        che1.setUserId(cho.getUserId());
        che1.setUserName(dataUser.getRealName());

        //车
        che1.setCarId(cho.getCarId());
        System.out.println(cho.getCarId());
        AjaxResult info3 = reCarApiService.getInfo(cho.getCarId(), SecurityConstants.INNER);
        LinkedHashMap cc = (LinkedHashMap) info3.get("data");
        CheCar cheCar = BeanUtil.mapToBean(cc, CheCar.class, true);
        che1.setCarNumber(cheCar.getCarNumber());//车牌

        che1.setCouponId(cho.getCouponId());//优惠价ID
        //服务项目
        List<CheServiceName> cheServiceNames = cheServiceMapper.selectOrderServiceNameById(cho.getId());

        String str=null;
        for (CheServiceName ser:cheServiceNames){
            str = ser.getName();
        }
        che1.setServiceName(str);//服务名称

        che1.setTotalPrice(cho.getTotalPrice());//总价
        che1.setRealPrice(cho.getRealPrice());//实付
//            BigDecimal realPrice = cho.getRealPrice();
//            BigDecimal coupon = big.subtract(realPrice);//优惠
//            che1.setCoupon(coupon);

        che1.setVerificationCode(cho.getVerificationCode());//核销码
        che1.setQrcode(cho.getQrcode());//核销二维码
        che1.setCreateTime(cho.getCreateTime());//下单时间
        che1.setSubscribeTime(cho.getSubscribeTime());//预约时间
        che1.setVerificationTime(cho.getVerificationTime());//核销时间
        che1.setRemark(cho.getRemark());//备注
        //状态
        String state=null;
        if(cho.getState().equals("0")){
            state="待支付";
        }
        if(cho.getState().equals("1")){
            state="待使用";
        }
        if(cho.getState().equals("2")){
            state="待评价";
        }
        if(cho.getState().equals("3")){
            state="已取消";
        }
        if(cho.getState().equals("4")){
            state="已退款";
        }
        if(cho.getState().equals("5")){
            state="已评价";
        }
        che1.setState(state);
        return che1;
    }

    //待支付---未成功！！
//    @Override
//    public List<CheOrderUserShop> selectOrderByNoPay(CheOrder cheOrder) {
//        ArrayList<CheOrderUserShop> list = new ArrayList<>();
//        List<CheOrder> list1 = cheOrderMapper.findOrderByNoPay(cheOrder);
//        for(CheOrder cho : list1){
//            CheOrderUserShop che1=new CheOrderUserShop();
//            AjaxResult info1 = cheApiShopService.getInfo(cho.getShopId(), SecurityConstants.INNER);
//            AjaxResult info2 = cheApiUserService.getInfo(cho.getUserId(), SecurityConstants.INNER);
//            //商家
//            LinkedHashMap ss=(LinkedHashMap)info1.get("data");
//            CheShop dataShop = BeanUtil.mapToBean(ss, CheShop.class, true);
//            //用户
//            LinkedHashMap uu = (LinkedHashMap)info2.get("data");
//            CheUser dataUser= BeanUtil.mapToBean(uu, CheUser.class, true);
//
//            che1.setId(cho.getId());
//            che1.setNumber(cho.getNumber());//订单号
//            che1.setShopId(cho.getShopId());//
//            che1.setShopName(dataShop.getShopName());//商家名
//            //用户
//            che1.setUserId(cho.getUserId());
//            che1.setUserName(dataUser.getRealName());
//            //车
//            che1.setCarId(cho.getCarId());
//            AjaxResult info3 = reCarApiService.getInfo(cho.getCarId(), SecurityConstants.INNER);
//            LinkedHashMap cc = (LinkedHashMap) info3.get("data");
//            CheCar cheCar = BeanUtil.mapToBean(cc, CheCar.class, true);
//            che1.setCarNumber(cheCar.getCarNumber());//车牌
//
//            che1.setCouponId(cho.getCouponId());//优惠价ID
//            //服务项目
//            List<CheServiceName> cheServiceNames = cheServiceMapper.selectOrderServiceNameById(cho.getId());
//
//            String str=null;
//            for (CheServiceName ser:cheServiceNames){
//                str = ser.getName();
////                Double newPrice = ser.getNewPrice();
////                ne+=newPrice;
//            }
//            che1.setServiceName(str);//服务名称
//
//            che1.setTotalPrice(cho.getTotalPrice());//总价
//            che1.setRealPrice(cho.getRealPrice());//实付
////            BigDecimal realPrice = cho.getRealPrice();
//
////            BigDecimal coupon = big.subtract(realPrice);//优惠
////            che1.setCoupon(coupon);
//            che1.setVerificationCode(cho.getVerificationCode());//核销码
//            che1.setQrcode(cho.getQrcode());//核销二维码
//            che1.setCreateTime(cho.getCreateTime());//下单时间
//            che1.setSubscribeTime(cho.getSubscribeTime());//预约时间
//            che1.setVerificationTime(cho.getVerificationTime());//核销时间
//
//            //状态
//            String state=null;
//            if(cho.getState().equals("0")){
//                state="待支付";
//            }
//            if(cho.getState().equals("1")){
//                state="待使用";
//            }
//            if(cho.getState().equals("2")){
//                state="待评价";
//            }
//            if(cho.getState().equals("3")){
//                state="已取消";
//            }
//            if(cho.getState().equals("4")){
//                state="已退款";
//            }
//            if(cho.getState().equals("5")){
//                state="已评价";
//            }
//            che1.setState(state);
//
//            list.add(che1);
//        }
//        return list;
//    }


}
